package net.xo68.destiny.starter.worker.handler.impl;

import net.xo68.destiny.contract.domain.job.TriggerParameter;
import net.xo68.destiny.contract.domain.report.DatagramSerializer;
import net.xo68.destiny.contract.domain.report.JobHandleReplyDatagram;
import net.xo68.destiny.core.timingwheel.process.EventProcessorContext;
import net.xo68.destiny.starter.worker.SchedulerWorkerProperties;
import net.xo68.destiny.starter.worker.handler.JobHandleReplyProvider;
import net.xo68.destiny.starter.worker.handler.JobHandler;
import net.xo68.destiny.starter.worker.logger.JobLogger;

/**
 * 任务处理器抽象类
 */
public abstract class AbstractJobHandler implements JobHandler {

    protected JobLogger jobLogger;

    protected final JobHandleReplyProvider jobHandleReplyProvider;
    protected final DatagramSerializer datagramSerializer;

    protected final SchedulerWorkerProperties schedulerWorkerProperties;

    public AbstractJobHandler(SchedulerWorkerProperties schedulerWorkerProperties,JobHandleReplyProvider jobHandleReplyProvider,DatagramSerializer datagramSerializer) {
        this.schedulerWorkerProperties = schedulerWorkerProperties;
        this.jobHandleReplyProvider=jobHandleReplyProvider;
        this.datagramSerializer=datagramSerializer;
    }

    @Override
    public void setJobLogger(JobLogger jobLogger) {
        this.jobLogger=jobLogger;
    }



    /**
     * 保存处理结果
     * @param processorContext
     * @param replyDatagram
     */
    protected void setHandleResult(final EventProcessorContext<TriggerParameter> processorContext, JobHandleReplyDatagram replyDatagram){
        processorContext.setResult(replyDatagram);
    }

    protected void publishDatagram(JobHandleReplyDatagram replyDatagram){
        if(this.jobHandleReplyProvider!=null){
            this.jobHandleReplyProvider.push(replyDatagram);
        }
    }
}
